package com.karson.admin.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.karson.common.model.BehaviorWeight;
import com.karson.common.model.EntityTypeDict;
import com.karson.common.request.PageParamRequest;
import com.karson.common.response.CommonResult;
import com.karson.service.service.EntityTypeDictService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author ZKS
 */
@RestController
@RequestMapping("/api/dict")
@Tag(name = "字典管理")
public class EntityDictController {
    @Autowired
    private EntityTypeDictService entityTypeDictService;

    @Operation(summary = "分页列表")
    @GetMapping("/list")
    public CommonResult<IPage<EntityTypeDict>> getList(@ModelAttribute @ParameterObject PageParamRequest pageParamRequest) {
        IPage<EntityTypeDict> pageParams = new Page<>(pageParamRequest.getCurrent(), pageParamRequest.getSize());
        IPage<EntityTypeDict> pageInfo =  entityTypeDictService.pageList(pageParams);
        return CommonResult.success(pageInfo);
    }
    @Operation(summary = "修改信息")
    @PostMapping("update")
    public CommonResult<String> update(@Validated @RequestBody EntityTypeDict entityTypeDict) {
        boolean update = entityTypeDictService.updateById(entityTypeDict);
        if (!update) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }
    @Operation(summary = "删除配置")
    @DeleteMapping("/del")
    public CommonResult<String> delete(@Validated @RequestBody EntityTypeDict entityTypeDict) {
        boolean remove = entityTypeDictService.removeById(entityTypeDict);
        if (!remove) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }
    @Operation(summary = "添加配置")
    @PostMapping("/add")
    public CommonResult<String> add(@Validated @RequestBody EntityTypeDict entityTypeDict) {
        boolean add = entityTypeDictService.save(entityTypeDict);
        if (!add) {
            return CommonResult.failed();
        }
        return CommonResult.success();
    }
    @Operation(summary = "回显表单")
    @GetMapping("/getDict")
    public CommonResult<EntityTypeDict> getBehaviorById(@RequestParam Integer id) {
        if (id == null || id <= 0) {return CommonResult.failed();}
        EntityTypeDict entityTypeDict = entityTypeDictService.getById(id);
        return CommonResult.success(entityTypeDict);
    }
}
